package com.demo.spring_demo.controller.song;

import com.demo.spring_demo.mapper.PlayListMapper;
import com.demo.spring_demo.pojo.PageInfo;
import com.demo.spring_demo.pojo.PlayList;
import com.demo.spring_demo.pojo.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/playlist")
public class PlayListController {

    //构造器装配
    @Resource(type= PlayListMapper.class)
    private PlayListMapper playListMapper;


    /**
     * 1、分页模糊查找歌曲
     * @param pageInfo
     * @return
     */
    @PostMapping("/selectplaylist")
    public Result selectplaylist(@RequestBody PageInfo<PlayList> pageInfo) {
        List<PlayList> playList = null;
        int count = 0;

        if ((count = playListMapper.getCount(pageInfo.getPlayList())) > 0 && (playList = playListMapper.getPlayList(pageInfo.getPlayList(), pageInfo.getPageIndex() * pageInfo.getPageSize(), pageInfo.getPageSize())) != null) {
            return new Result(200, new PageInfo<PlayList>(pageInfo.getPageIndex(), pageInfo.getPageSize(), playList, count));
        } else {
            return new Result(500, "信息不存在！");
        }
    }


    /**
     * 1、分页模糊查找歌曲
     * @param playListId
     * @return
     */
    @GetMapping("/delplaylist/{playListId}")
    public Result delPlayList(@PathVariable int playListId) {
        if(playListId > 0 && playListMapper.deletePlayList(playListId) > 0) {
            return new Result(200,"success");
        }
        return new Result(500,"error");
    }
}
